<?php
namespace app\api\controller;

use think\facade\Config;
use app\api\controller\Base;
use app\goods\model\ImsEweiShopGoods;
use app\member\model\ImsMcMembers;
use app\member\model\ImsEweiShopMember;
use app\member\model\ImsEweiShopMemberCart;

class Cart extends Base
{
    public function index()
    {
        $token = $this->request->get('token');
        $member = $this->getMemberInfoByToken($token);
        if (empty($member)) {
            $this->echojson(1,'登陆过期，请重新登陆', $token);
        }

        $carWhere = [
            'f.openid' => $member['openid'],
            'f.deleted' => 0
        ];

        $fields = " f.id,f.selected AS `select`,f.goodsid AS goods_id,f.total AS goods_number,g.thumb,g.title,g.total AS number_stock,g.marketprice AS price,g.total,g.isnew,g.ishot,g.isdiscount,g.isrecommand ";

        $cart_list = ImsEweiShopMemberCart::mk()->alias('f')->leftJoin('ims_ewei_shop_goods g ','f.goodsid = g.id')->field($fields)->where($carWhere)->select();

        $data = [
            "cart_list"=> ImsEweiShopMemberCart::formatDatas($cart_list),
            'exp_info'=>[
                'cha'=>0
            ]
        ];
        $this->echojson(0,'获取商品信息成功', $data);
    }

    public function addcart(){

        $goods_id = $this->request->post('goods_id');
        $list_id = $this->request->post('list_id');
        $total = $this->request->post('goods_number');
        $token = $this->request->post('token');
        $optionid = $this->request->post('optionid');
        $diyformid = 0;
        $diyformfields = iserializer(array());
        $diyformdata = iserializer(array());

        $goods = ImsEweiShopGoods::mk()->where(['id'=>$goods_id])->find();

        if (empty($goods)) {
            $this->echojson(1,'商品不存在或已下架，请刷新后再试', []);
        }

        if (0 < $goods['hasoption'] && empty($optionid)) {
            $this->echojson(1,'请选择规格', []);
        }

        $member = $this->getMemberInfoByToken($token);
        if (empty($member)) {
            $this->echojson(1,'登陆过期，请重新登陆', $token);
        }

        if ($goods['isverify'] == 2 || $goods['type'] == 2 || $goods['type'] == 3) {
            $this->echojson(1,'此商品不可加入购物车,请直接点击立刻购买', $goods);
        }

        if($goods['total'] <= 0){
            $this->echojson(1,'库存不足', $goods);
        }

        $dataWhere = [
            'goodsid'=>$goods_id,
            'openid'=>$member['openid'],
            'optionid'=>$optionid,
            'deleted'=>0,
        ];
        $data = ImsEweiShopMemberCart::mk()->where($dataWhere)->find();

        if (empty($data)) {
            $data = ImsEweiShopMemberCart::mk()->create([
                'uniacid' => 2, 
                'merchid' => $goods['merchid'], 
                'openid' => $member['openid'], 
                'goodsid' => $goods_id, 
                'optionid' => $optionid, 
                'marketprice' => $goods['marketprice'], 
                'total' => $total, 
                'selected' => 1, 
                'diyformid' => $diyformid, 
                'diyformdata' => $diyformdata, 
                'diyformfields' => $diyformfields, 
                'createtime' => time()
            ]);
        }else {
            $data['diyformid'] = $diyformid;
            $data['diyformdata'] = $diyformdata;
            $data['diyformfields'] = $diyformfields;
            ImsEweiShopMemberCart::mk()->update([
                'id' => $data['id'],
                'diyformid' => $diyformid, 
                'diyformdata' => $diyformdata, 
                'diyformfields' => $diyformfields, 
                'total' => $data['total'] += $total, 
            ]);
        }

        $data = ImsEweiShopMemberCart::mk()->where($dataWhere)->find();

        $this->echojson(0,'购物车添加成功', $data);
    }

    public function changcart(){
        $token = $this->request->post('token');
        $id = $this->request->post('id');
        $total = $this->request->post('goods_number');
        $type = $this->request->post('type')?$this->request->post('type'):1; //1改数量2改选中状态
        $selected = $this->request->post('selected')?$this->request->post('selected'):0;

        if ($id>0 && $total>0 && $type==1 ) {
            ImsEweiShopMemberCart::mk()->update([
                'id' => $id,
                'total' => $total 
            ]);
        }elseif ($type==2) {
            ImsEweiShopMemberCart::mk()->update([
                'id' => $id,
                'selected' => $selected 
            ]);
        }
        
        $this->echojson(0,'成功', []);
    }

    public function removecart(){
        $token = $this->request->post('token');
        $id = $this->request->post('ids');
        if ($id>0) {
            ImsEweiShopMemberCart::mk()->update([
                'id' => $id,
                'deleted' => 1 
            ]);
        }

        $this->echojson(0,'商品删除成功', []);
    }

}